package com.comp490.bullet;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;

import com.comp490.pattern.BulletPattern;
import com.comp490.pattern.ConePattern;
import com.comp490.shooter.GameView;
import com.comp490.sound.SoundManager;
import com.comp490.sprite.EnemySprite;
import com.comp490.sprite.ShipSprite;

public class PlayerBulletPattern
{
    private int fcount;
    private int fireRate;
    private int next;
    private GameView gameView;
    private ShipSprite playerShip;
    private List<BulletPattern> bPatterns;
    private List<EnemySprite> enemies;

    public PlayerBulletPattern(int fr, GameView gv, ShipSprite pS)
    {
        fcount = 0;
        fireRate = fr;
        playerShip = pS;
        next = 0;
        gameView = gv;
        bPatterns = new ArrayList<BulletPattern>(40);
        for(int i = 0; i < 20; i++)
            bPatterns.add(genPattern());
    }

    private BulletPattern genPattern()
    {
        List<Bullet> bullets = new ArrayList<Bullet>(3);
        for(int i = 0; i < 3; i++)
            bullets.add(new YellowPlayerBullet(-100, -100, 1, false));
        
        return new ConePattern(6, bullets, gameView, false, 20, 0);
    }

    public void setOnScreenEnemies(List<EnemySprite> enemies)
    {
        this.enemies = enemies;
    }

    public void updatePosition()
    {
        if(fcount++ % fireRate == 0 && playerShip.isActive())
        {
            BulletPattern pat = bPatterns.get(next);

            if(pat.isActive())
            {
                BulletPattern temp = genPattern();
                temp.setCoordinates(playerShip.getTipX(), playerShip.getTipY());
                temp.reset();
                bPatterns.add(next++, temp);
                System.out.println("NEW PAT");
            }
            else
            {
                pat.setCoordinates(playerShip.getTipX(), playerShip.getTipY());
                pat.reset();
//                System.out.println("RECYCLE");
                next = ++next % bPatterns.size();
            }
//            System.out.println(next);
            
            // sound fx played when ship fires bullet
            SoundManager.playSoundEfx(SoundManager.PLAYER_FIRE);
        }
        
        for(int i = 0; i < bPatterns.size(); i++)
        {
        	bPatterns.get(i).moveAndCheckCollision(enemies);
        }
    }

    public void draw(Canvas c)
    {
    	for(int i = 0; i < bPatterns.size(); i++)
    	{
    		 if(bPatterns.get(i).isActive())
    			 bPatterns.get(i).draw(c);
    	}
    }
}
